﻿using Microsoft.AspNetCore.Http;
using ThresholdAssistant.Tools;
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;

namespace ThresholdAssistant.Http.Extensions
{
    /// <summary>
    /// Http客户端扩展
    /// </summary>
    public static class HttpClientExtensions
    {
        /// <summary>
        /// 最小版本号
        /// </summary>
        private static readonly string minVersion = "2.0";

        /// <summary>
        /// 获取响应内容
        /// </summary>
        /// <param name="response"></param>
        /// <returns></returns>
        public static async Task<string> GetResponseBody(this HttpResponse response)
        {
            string text = "";
            if (response == null)
                return text;

            response.Body.Seek(0, SeekOrigin.Begin);
            text = await new StreamReader(response.Body).ReadToEndAsync();
            response.Body.Seek(0, SeekOrigin.Begin);
            return text;
        }

        /// <summary>
        /// 获取请求参数
        /// </summary>
        /// <param name="request">请求参数</param>
        /// <returns></returns>
        public static async Task<string> GetRequestBody(this HttpRequest request)
        {
            if (request == null)
                return "";

            request.EnableBuffering();
            var sream = new StreamReader(request.Body);
            var body = await sream.ReadToEndAsync();
            if (request.ContentType == "application/x-www-form-urlencoded")
            {
                body = WebUtility.UrlDecode(body);
            }
            request.Body.Position = 0;
            return body;
        }

        /// <summary>
        /// 获取业务流水号
        /// </summary>
        /// <param name="headers"></param>
        /// <returns></returns>
        public static string GetSerialNo(this IHeaderDictionary headers)
        {
            var serialNo = SnowflakeNoUtil.GetSerialNo();
            if (headers.ContainsKey("x-traceId-header"))
            {
                serialNo = headers["x-traceId-header"];
            }
            else
            {
                headers.Add("x-traceId-header", serialNo);
            }
            return serialNo;
        }

        /// <summary>
        /// 检查商家使用的版本是否大于等于2.0的版本
        /// </summary>
        /// <param name="headers">头部信息</param>
        /// <returns></returns>
        public static bool IsGteVersion2(this IHeaderDictionary headers)
        {
            var isSuccess = false;
            try
            {
                if (headers == null)
                    return isSuccess;

                if (headers.ContainsKey("x-version"))
                {
                    var version = new Version(headers["x-version"]);
                    if (version >= new Version(minVersion))
                    {
                        isSuccess = true;
                    }
                }
            }
            catch (Exception)
            {

            }
            return isSuccess;
        }
    }
}